package xyz.dava.sync.fm.vo;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.List;

import java.util.Date;

/**
 * Bom物料
 */
@Data
@NoArgsConstructor
@Document("bom_ms")
public class BomMsTO {

    @Id
    private String id;

    /**
     * 物料号
     */
    private String msCode;

    /**
     * 显示物料号
     */
    private String msCodeView;

    /**
     * 父级物料号
     */
    @Transient
    private String parentBomCode;

    /**
     * 物料名称
     */
    private String msName;

    /**
     * 路线（前驱）
     */
    private String wegxx;

    /**
     * BOM 类别
     */
    private String bomType;

    /**
     * BOM用途
     */
    private String bomUsage;

    /**
     * 所属工厂
     */
    private String factory;

    /**
     * 标准价格
     */
    private String StanderPrice;

    /**
     * 单位
     */
    private String units;

    /**
     * 移动平均价格
     */
    private String movingAveragePrice;

    /**
     * 没有说明
     */
    private String level;

    /**
     * 树结构
     * eg:Q0D361F1559{{0010}}Q0C361F1561
     */
    private String level1;

    /**
     * 备注
     */
    private String remark;

    private List<BomFileVO> files;

    /**
     * 父节点
     */
    private List<Parent> parentCode;
    /**
     * 子节点
     */
    private List<Children> childrenCode;

    /**
     * 子节点个数
     */
    private Long childrenCount;

    private List<BomMsRouteVO> routes;

    private String updatedDate;

    public static String processMsCode(String msCode) {
        if (StrUtil.isBlank(msCode)) {
            return "";
        }
        char[] chars = msCode.toCharArray();
        int index = 0;
        while (chars[index] == '0') {
            index++;
        }
        return msCode.substring(index);
    }

    @Data
    public static class Parent {
        private String parentId;
        private String msCode;
        private String msName;

        public Parent() {
        }

        public Parent(BomMsTO to) {
            this.parentId = to.getId();
            this.msCode = to.getMsCode();
            this.msName = to.getMsName();
        }

        @Override
        public int hashCode() {
            return msCode.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (this == obj) {
                return true;
            }
            if (obj instanceof Parent) {
                return obj.toString().equals(obj.toString());
            }
            return false;
        }

        @Override
        public String toString() {
            return "Parent{" +
                    "parentId='" + parentId + '\'' +
                    ", msCode='" + msCode + '\'' +
                    ", msName='" + msName + '\'' +
                    '}';
        }
    }

    @Data
    public static class Children {
        /**
         * 原始数据
         */
        private String msCode;
        private Double needCount;
        private String remark;
        private boolean isDelete;
    }
}
